<xsl:stylesheet version="1.0"
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
				xmlns:date="http://exslt.org/dates-and-times"
				extension-element-prefixes="date">
<!--
Copyright 2011, 2012 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<xsl:variable name="safe">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz</xsl:variable>

<xsl:template name="package-name">
	<xsl:param name="operation"/>
	<xsl:param name="seq"/>
	<xsl:value-of select="/package/owner/shortname"/>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="/package/delivery/identifier"/>
	<xsl:text>-</xsl:text>	
	<xsl:value-of select="/package/metadata/vendor_id"/>
	<xsl:text>-</xsl:text>
	<xsl:call-template name="locale-list-stripped"/>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="$operation"/>
	<xsl:text>-</xsl:text>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:year()"/></xsl:with-param>
		<xsl:with-param name="length">4</xsl:with-param>
	</xsl:call-template>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:month-in-year()"/></xsl:with-param>
		<xsl:with-param name="length">2</xsl:with-param>
	</xsl:call-template>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:day-in-month()"/></xsl:with-param>
		<xsl:with-param name="length">2</xsl:with-param>
	</xsl:call-template>
	<xsl:text>T</xsl:text>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:hour-in-day()"/></xsl:with-param>
		<xsl:with-param name="length">2</xsl:with-param>
	</xsl:call-template>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:minute-in-hour()"/></xsl:with-param>
		<xsl:with-param name="length">2</xsl:with-param>
	</xsl:call-template>
	<xsl:call-template name="zero-pad">
		<xsl:with-param name="value"><xsl:value-of select="date:second-in-minute()"/></xsl:with-param>
		<xsl:with-param name="length">2</xsl:with-param>
	</xsl:call-template>
	<xsl:if test="$seq">
		<xsl:text>-</xsl:text>
		<xsl:value-of select="$seq"/>
	</xsl:if>
</xsl:template>

<xsl:template name="featuretrailerposter-filename">
	<xsl:param name="seq"/>
	<xsl:param name="filename"/>
	<xsl:param name="type"/>
	<xsl:value-of select="/package/metadata/vendor_id"/>
	<xsl:text>-</xsl:text>
	<xsl:call-template name="locale-list-stripped"/>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="$type"/>
	<xsl:text>-</xsl:text>
	<xsl:call-template name="feature-identifier"/>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="$seq"/>
	<xsl:text>.</xsl:text>
	<xsl:call-template name="get-file-extension">
		<xsl:with-param name="path"><xsl:value-of select="$filename"/></xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template name="caption-filename">
	<xsl:param name="language"/>
	<xsl:param name="seq"/>
	<xsl:param name="filename"/>
	<xsl:value-of select="/package/metadata/vendor_id"/>
	<xsl:text>-</xsl:text>
	<xsl:call-template name="locale-list-stripped"/>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="$language"/>
	<xsl:text>-</xsl:text>
	<xsl:text>subtitles</xsl:text>
	<xsl:text>-</xsl:text>
	<xsl:value-of select="$seq"/>
	<xsl:text>.</xsl:text>
	<xsl:call-template name="get-file-extension">
		<xsl:with-param name="path"><xsl:value-of select="$filename"/></xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template name="locale-list">
	<!-- generate a list of locales -->
	<!-- TODO: remove the final space character -->
	<xsl:for-each select="/package/locale">
		<xsl:value-of select="@country"/><xsl:text disable-output-escaping="yes">&#32;</xsl:text>
	</xsl:for-each>
</xsl:template>	

<xsl:template name="locale-list-stripped">
	<!-- generate a list of locales -->
	<xsl:for-each select="/package/locale">
		<xsl:value-of select="@country"/>
	</xsl:for-each>
</xsl:template>	

<xsl:template name="currency-for-locale">
	<xsl:param name="locale"/>
	<xsl:choose>
		<xsl:when test="$locale='GB'">GBP</xsl:when>
		<xsl:when test="$locale='FR'">EUR</xsl:when>
		<xsl:when test="$locale='NL'">EUR</xsl:when>
		<xsl:when test="$locale='IT'">EUR</xsl:when>
		<xsl:when test="$locale='ES'">EUR</xsl:when>
		<xsl:when test="$locale='JP'">JPY</xsl:when>
		<xsl:when test="$locale='DE'">EUR</xsl:when>
		<xsl:when test="$locale='US'">USD</xsl:when>
		<xsl:when test="$locale='CA'">CAD</xsl:when>
		<xsl:otherwise><xsl:message terminate="yes">Invalid locale '<xsl:value-of select="$locale"/>' in template 'currency-for-locale'</xsl:message></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="watch-duration-for-locale">
	<xsl:param name="locale"/>
	<xsl:choose>
		<xsl:when test="$locale='GB'">48</xsl:when>
		<xsl:when test="$locale='FR'">48</xsl:when>
		<xsl:when test="$locale='ES'">48</xsl:when>
		<xsl:when test="$locale='JP'">48</xsl:when>
		<xsl:when test="$locale='DE'">48</xsl:when>
		<xsl:when test="$locale='US'">24</xsl:when>
		<xsl:when test="$locale='CA'">48</xsl:when>
		<xsl:otherwise><xsl:message terminate="yes">Invalid locale '<xsl:value-of select="$locale"/>' in template 'watch-duration-for-locale'</xsl:message></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="original-title-for-language">
	<xsl:param name="language"/>
	<xsl:choose>
		<xsl:when test="$language='en'">Original Title</xsl:when>
		<xsl:when test="$language='fr'">Titre original</xsl:when>
		<xsl:when test="$language='es'">Título original</xsl:when>
		<xsl:when test="$language='it'">Titolo originale</xsl:when>
		<xsl:when test="$language='ja'">原作</xsl:when>
		<xsl:when test="$language='de'">Originaltitel</xsl:when>
		<xsl:otherwise><xsl:message terminate="yes">Invalid language '<xsl:value-of select="$locale"/>' in template 'original-title-for-language'</xsl:message></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="trailer-for-language">
	<xsl:param name="language"/>
	<xsl:choose>
		<xsl:when test="$language='en'">Trailer</xsl:when>
		<xsl:when test="$language='fr'">Bande Annonce</xsl:when>
		<xsl:when test="$language='es'">Tráiler</xsl:when>
		<xsl:when test="$language='it'">Trailer</xsl:when>
		<xsl:when test="$language='ja'">予告編</xsl:when>
		<xsl:when test="$language='de'">Trailer</xsl:when>
		<xsl:otherwise><xsl:message terminate="yes">Invalid language '<xsl:value-of select="$locale"/>' in template 'trailer-for-language'</xsl:message></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="dubbedflag-for-language">
	<xsl:param name="language"/>
	<xsl:choose>
		<xsl:when test="$language='en'">(Dubbed)</xsl:when>
		<xsl:when test="$language='fr'">(VF)</xsl:when>
		<xsl:when test="$language='es'">(VE)</xsl:when>
		<xsl:when test="$language='it'"></xsl:when>
		<xsl:when test="$language='ja'">(吹き替え）</xsl:when>
		<xsl:when test="$language='de'">(dt.)</xsl:when>
		<xsl:otherwise><xsl:message terminate="yes">Invalid language '<xsl:value-of select="$locale"/>' in template 'dubbedflag-for-language'</xsl:message></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="get-file-extension">
	<!-- given a file path, returns outputs the substring after a character, but scanning from the end -->
	<!-- of the string rather than from the start -->
	<xsl:param name="path"/>
	<xsl:choose>
		<xsl:when test="contains($path, '/')">
			<xsl:call-template name="get-file-extension">
				<xsl:with-param name="path" select="substring-after($path, '/')" />
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="last-substring-after">
				<xsl:with-param name="char">.</xsl:with-param>
				<xsl:with-param name="value" select="$path"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="last-substring-after">
	<!-- outputs the substring after a character, but scanning from the end -->
	<!-- of the string rather than from the start -->
	<xsl:param name="value"/>
	<xsl:param name="char"/>
	<xsl:param name="position" select="string-length($value)"/>
	<xsl:choose>
		<xsl:when test="not(contains($value,$char))"><!-- empty string --></xsl:when>
		<xsl:when test="substring($value,$position,1)=$char">
			<xsl:value-of select="substring($value,$position + 1,string-length($value) - $position)"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="last-substring-after">
				<xsl:with-param name="value" select="$value"/>
				<xsl:with-param name="char" select="$char"/>
				<xsl:with-param name="position" select="$position - 1"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="zero-pad">
	<!-- recursive template to right justify and prepend zeroes -->
	<xsl:param name="char">0</xsl:param>
	<xsl:param name="value"/>
	<xsl:param name="length"/>
	<xsl:choose>
		<xsl:when test="string-length($value) &lt; $length">
			<xsl:call-template name="zero-pad">
				<xsl:with-param name="char" select="$char"/>
				<xsl:with-param name="value" select="concat($char,$value)"/>
				<xsl:with-param name="length" select="$length"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="substring($value,string-length($value) - $length + 1)"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="feature-identifier">
	<xsl:call-template name="make-safe-string">
		<xsl:with-param name="str" select="/package/metadata/title"/>
	</xsl:call-template>
</xsl:template>

<xsl:template name="make-safe-string">
	<xsl:param name="str"/>
	<xsl:if test="$str">
		<xsl:variable name="first-char" select="substring($str,1,1)"/>
		<xsl:choose>
			<xsl:when test="contains($safe,$first-char)">
				<xsl:value-of select="$first-char"/>
			</xsl:when>
		</xsl:choose>
		<xsl:if test="string-length($str) > 1">
			<xsl:call-template name="make-safe-string">
				<xsl:with-param name="str" select="substring($str,2)"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
